#!/bin/bash

#=======================================================================
# ga-sjekk
# File ID: 4d412868-f13b-11e4-be3f-000df06acc56
#
# Check if the specified files are already archived in git-annex. Files 
# that are already archived are moved to ".ga-found/$base/".
#
# Author: Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later.
#=======================================================================

progname=ga-sjekk

for f in "$@"; do
    key=$(ga-key -q "$f")
    test -n "$key" && {
        echo ======== $f $key
        # FIXME: Hardcoding
        for dir in innsp musikk opptak s usortert; do
            if test -d "/media/seagate-3tb/annex/$dir"; then
                dir="/media/seagate-3tb/annex/$dir"
            elif test -d "/home/sunny/annex/$dir"; then
                dir="/home/sunny/annex/$dir"
            elif test -d "/media/pavi/annex/$dir"; then
                dir="/media/pavi/annex/$dir"
            else
                echo $progname: $dir: Repo not found >&2
                continue
            fi
            (
                cd "$dir" && {
                    result="$(ga whereis --key "$key" 2>/dev/null | grep -v '(0 copies) failed$')"
                    test -n "$result" && {
                        echo
                        echo $dir
                        base=$(basename "$dir")
                        ga whereis --key "$key"
                        cd -
                        dest=".ga-found/$base/$(dirname "$f")"
                        mkdir -p "$dest"
                        mv -vi "$f" "$dest"
                    }
                }
            )
        done
    }
done
